//app.js
//var Parser = require('./lib/dom-parser')

App({
  onLaunch: function() {
    // 展示本地存储能力
    var thisApp = this
    var logs = wx.getStorageSync('logs') || []
    logs.unshift(Date.now())
    wx.setStorageSync('logs', logs)

    // 登录
    wx.login({
      success: res => {
        // 发送 res.code 到后台换取 openId, sessionKey, unionId
      }
    })
    // 获取用户信息
    wx.getSetting({
      success: res => {
        if (res.authSetting['scope.userInfo']) {
          // 已经授权，可以直接调用 getUserInfo 获取头像昵称，不会弹框
          wx.getUserInfo({
            success: res => {
              // 可以将 res 发送给后台解码出 unionId
              this.globalData.userInfo = res.userInfo

              // 由于 getUserInfo 是网络请求，可能会在 Page.onLoad 之后才返回
              // 所以此处加入 callback 以防止这种情况
              if (this.userInfoReadyCallback) {
                this.userInfoReadyCallback(res)
              }
            }
          })
        }
      }
    })
    wx.getStorage({
      key: 'history',
      success: function(res) {
        thisApp.globalData.history_list = res.data
      },
    })

  },
  //删除历史记录
  deleteHistoryItem: function(id) {
    var thisApp = this
    var list = thisApp.globalData.history_list
    //var x
    for (var i in list.data) {
      if (id === list.data[i].id) {
        //console.log(x)
        list.data.splice(i, 1)
        this.globalData.history_list.count = list.count - 1
        this.globalData.history_list.data = list.data
      }
    }
    //console.log(list)
    console.log(this.globalData.history_list)
    wx.setStorage({
      key: 'history',
      data: thisApp.globalData.history_list,
    })
  },
  //添加历史记录
  addHistoryList: function(item) {
    var thisApp = this
    var list = thisApp.globalData.history_list
    var flag = 0 //查找
    if (list.count == 0) {
      this.globalData.history_list.count = 1
      this.globalData.history_list.data = list.data.concat(item)
    } else {
      for (var i in list.data) {
        if (item.id === list.data[i].id) {
          //flag = 1
          list.data.splice(i, 1)
          this.globalData.history_list.count = list.count - 1
          this.globalData.history_list.data = list.data
          break
        }
      }
      if (flag == 0) {
        this.globalData.history_list.count = list.count + 1
        this.globalData.history_list.data = list.data.concat(item)
      }
    }
    //console.log(this.globalData.history_list)
    //console.log(typeof(list))
    //console.log(list)
    wx.setStorage({
      key: 'history',
      data: thisApp.globalData.history_list,
      success: function(res) {
        //console.log(res)
      }
    })
  },
  getDanmuList: function(vid, idx) {
    var xml
    var thisApp = this
    var list = thisApp.globalData.danmu_video
    for (var i in list) {
      if (vid == list[i].id) {
        var cid = list[i].cid[idx]
        wx.request({
          url: 'https://comment.bilibili.com/' + cid + '.xml',
          method: 'GET',
          header: {
            'Content-Type': 'text/javascript'
          },
          success: function(res) {
            //console.log(res)
            console.log(res.data)
            xml = res.data
          }
        })

        return xml
      }
    }
    return null
  },
  globalData: {
    userInfo: null,
    history_list: {
      count: 0,
      data: []
    },
    danmu_video: [{
        id: '92f5255ab228040f3dceed9c7fe740b4',
        name: '',
        cid: ["100933579", "102590394", "104823788", "105888883", "108298702", "109548221", "111838614", "113515423", "119154515", "115891131", "117184794", "118516951"]
      },

      {
        id: "e7dc90f9c38a568e25d2c062a516a3b5",
        name: "鬼灭之刃",
        cid: ["125463156", "93557037", "87351529", "103750248", "92472194", "92472373", "92468637", "93640630", "95287956", "95941164", "97118642", "98599834", "103750897", "101315749", "102665280", "104221434", "107591853", "107630711", "109424731", "111387126", "112918029", "114543803", "116061793", "117207170", "118572350", "120076716"]
      },
      {
        id: "d016a6c2e90831903cb7e19e081b25f8",
        name: "你的名字",
        cid: ["126654047"]
      },
      {
        id: '9b8f3a929d7345b16257fb88cd9dea1e',
        name: '这个勇者明明超强却过分慎重',
        cid: ["121158988", "123556724", "125243093", "126495971", "127889933", "129264847"]
      }
      // {
      //   id:"",
      //   name:"",
      //   cid:[]
      // }
    ]
  }
})